package com.wubo.sec.ex;

import com.wubo.sec.core.UserData;
import com.wubo.sec.core.UserLoginProcessor;
import com.wubo.sec.model.User;
import com.wubo.sec.service.IUserService;

public abstract class AbstractUserLoginProcessor implements UserLoginProcessor {

	protected IUserService userService;
	
	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public UserData login(String username) {
		beforeLogin(username);
		
		UserData user = doLogin(username);
		
		afterLogin(user);
		
		return user;
	}

	public void onUserLoginSuccess(UserData user, String ip) {
		beforeLoginSuccess(user, ip);
		
		doOnUserLoginSuccess(user, ip);
		
		afterLoginSuccess(user, ip);
	}

	public boolean supports(Class<? extends UserData> userClass) {
		return User.class.isAssignableFrom(userClass);
	}
	
	protected UserData doLogin(String username){
		return userService.getByUsername(username);
	}
	
	protected void doOnUserLoginSuccess(UserData user, String ip){
		userService.onUserLoginSuccess(user, ip);
	}
	
	protected void beforeLogin(String username){
		
	}
	
	protected void afterLogin(UserData user){
		
	}
	
	protected void beforeLoginSuccess(UserData user, String ip){
		
	}
	
	protected void afterLoginSuccess(UserData user, String ip){
		
	}
	
}
